package com.service.medical.Impl;

import com.comm.R;
import com.entity.DrugTable;
import com.github.pagehelper.PageHelper;
import com.mapper.medical.DrugMapper;
import com.service.medical.DrugService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DrugServiceImpl implements DrugService {
    @Resource
    private DrugMapper drugMapper;

    public R findDrugList(int currentPage,int pageSize,String drugName,String drugSort,int status){
        R r = new R();
        try {
        com.github.pagehelper.Page<DrugTable> page = PageHelper.startPage(currentPage,pageSize);
        List<DrugTable> drugTableList = drugMapper.findDrugList(drugName,drugSort,status);
        long count =  page.getTotal();
        System.out.println("总记录数:"+count);
            if(count>0){
                r.setCode(200);
                r.setData(drugTableList);
            }else {
                r.setCode(500);
                r.setData("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }

    @Override
    public R findDrugById(String drug_id) {
        R r = new R();
        try {
            List<DrugTable> drupList = drugMapper.findDrugById(drug_id);
            System.out.println(drupList.size());
            int count = drupList.size();
            if(count>0){
                r.setCode(200);
                r.setData(drupList);
            }else {
                r.setCode(500);
                r.setData("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }

    @Override
    public R addDrug(int id) {
        R r = new R();
        try {
            Integer result = drugMapper.addDrug(id);
            if(result>0){
                r.setCode(200);
                r.setData("success");
            }else {
                r.setCode(500);
                r.setData("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }

    @Override
    public R deleteDrug(int id) {
        R r = new R();
        try {
            Integer result = drugMapper.deleteDrug(id);
            if(result>0){
                r.setCode(200);
                r.setData("success");
            }else {
                r.setCode(500);
                r.setData("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }

}
